#include "watchlist.hpp"
#include "ui_watchlist.h"

WatchList::WatchList(QWidget *parent) :
    QDockWidget(parent),
    ui(new Ui::WatchList)
{
    ui->setupUi(this);
    ui->tbl_watchlist->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
}


WatchList::~WatchList()
{
    delete ui;
}


void WatchList::changeEvent(QEvent *e)
{
    QDockWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}


void WatchList::reset()
{
    this->variableNameMap.clear();
    this->variableValue1Map.clear();
    this->variableValue2Map.clear();

    while (!this->itemList.isEmpty())
	delete this->itemList.takeFirst();
}


void WatchList::onVariableAdded(QString name, QString address)
{
    int adr = address.toInt(0, 16) & 127;

    //Save Address and Name
    if (this->variableNameMap.contains(adr))
	this->variableNameMap.remove(adr);

    this->variableNameMap.insert(adr, name);


    //Save Address and default value
    if (this->variableValue1Map.contains(adr))
    {
	this->variableValue1Map.remove(adr);
	this->variableValue2Map.remove(adr);
    }

    this->variableValue1Map.insert(adr, "00");
    this->variableValue2Map.insert(adr, "00");

    this->updateView();
}


void WatchList::onVariableChanged(Register *reg)
{
    int address = reg->getAddress();
    int bank = reg->getBank();

    if (!this->variableNameMap.contains(address))
	return;

    if (bank == 0 || bank == 2)
    {
	if (this->variableValue1Map.contains(address))
	{
	    this->variableValue1Map.remove(address);
	    this->variableValue1Map.insert(address, reg->toString());
	}
    } else if (bank == 1) {
	if (this->variableValue2Map.contains(address))
	{
	    this->variableValue2Map.remove(address);
	    this->variableValue2Map.insert(address, reg->toString());
	}
    }

    QString name = this->variableNameMap.find(address).value();
    QString adr = QString::number(address, 16).toUpper();
    if (adr.size() < 2)
	adr.prepend("0");
    QString tip = "Register Address: " + adr + " - Value: " + reg->toString();
    emit variableTooltipChanged(name, tip);

    this->updateView();
}


void WatchList::updateView()
{
    while (!this->itemList.isEmpty())
	delete this->itemList.takeFirst();

    QMapIterator<int, QString> it(this->variableNameMap);

    int row = 0;
    while (it.hasNext())
    {
	it.next();
	ui->tbl_watchlist->setRowCount(row + 1);

	QString adr = QString::number(it.key(), 16).toUpper();
	if (adr.size() < 2)
	    adr.prepend("0");

	QTableWidgetItem *item = new QTableWidgetItem("0x" + adr);
	ui->tbl_watchlist->setItem(row, 0, item);
	this->itemList.append(item);

	item = new QTableWidgetItem(it.value());
	ui->tbl_watchlist->setItem(row, 1, item);
	this->itemList.append(item);

	item = new QTableWidgetItem(this->variableValue1Map.find(it.key()).value());
	ui->tbl_watchlist->setItem(row, 2, item);
	this->itemList.append(item);

	item = new QTableWidgetItem(this->variableValue2Map.find(it.key()).value());
	ui->tbl_watchlist->setItem(row, 3, item);
	this->itemList.append(item);

	row++;
    }
}
